package com.ghsc.ghxc.jsb.ui.live.activity

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.widget.CustomRefreshListener
import com.cn.appcore.widget.RefreshHelper
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.ActivityRedIndexBinding
import com.ghsc.ghxc.jsb.ui.live.bean.RedDataBean
import com.scwang.smart.refresh.layout.api.RefreshLayout

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.live.activity
 * 类名：RedExtensionIndexActivity
 * 创建时间：2024/5/23 19:06
 * 创建人 @author： xiaolin
 * 描述 ：Todo...红包list
 */
class RedExtensionIndexActivity  :
    BaseVMActivity<RedExtensionIndexVideModel, ActivityRedIndexBinding>(){
    override fun attachLayoutRes(): Int {
        return R.layout.activity_red_index
    }
    override fun attachVMClass(): Class<RedExtensionIndexVideModel>? {
        return  RedExtensionIndexVideModel::class.java
    }

    private val mAdapter: RedExtensionListAdapter by lazy { RedExtensionListAdapter() }
    var mList = arrayListOf<RedDataBean>()

    var  bundle  : Bundle?=null

    var packetid:String =""
    var targetId:String =""

    var userId = UserManager.getUserId()







    override fun initView() {
        bundle=intent.extras
        packetid= bundle!!.getString("packetid").toString()
        targetId= bundle!!.getString("targetId").toString()
        binding.rvList.layoutManager = LinearLayoutManager(this)
        binding.rvList.adapter = mAdapter
        mAdapter.setNewInstance(mList)

        initRefreshLayout()
        getListData()






    }

    override fun initData() {
        super.initData()
        binding.topTitle.text="红包详情"
        binding.topLeftView.setSingleClickListener { finish() }

    }

    private fun initRefreshLayout() {
        binding.refreshLayout.setEnableLoadMore(false)
        RefreshHelper.initRefreshLayout(binding.refreshLayout, object : CustomRefreshListener {
            override fun _onRefresh(refreshLayout: RefreshLayout?) {
                getListData()
            }
            override fun _onLoadMore(refreshLayout: RefreshLayout?) {
            }
        })
    }
    private fun getListData() {
        val map: HashMap<String, Any> = HashMap()
        map["packet_id"] = packetid
//        map["ptype"] = ptype
//        map["gid"] = targetId
//        map["pindex"] = pageNo
//        map["psize"] = pageSize
        GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onPacketDetailMethod(it1) }
    }


    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@RedExtensionIndexActivity) {
                binding.refreshLayout.finishRefresh()
                showToast(it)
                finish()
            }
            againLoginMsg.observe(this@RedExtensionIndexActivity)  {
                binding.refreshLayout.finishRefresh()
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()

            }
            onPacketDetailData.observe(this@RedExtensionIndexActivity){
                binding.refreshLayout.finishRefresh()
                binding.run {
                    GlideUtil.ImageHeadLoad(this@RedExtensionIndexActivity,it.data.packet.send_user_headimg,headimg)
                    nickname.text=it.data.packet.send_user_nickname+"发出的红包"
                    memo.text =it.data.packet.memo
                    tvType.text=it.data.packet.ptype
                    if (it.data.self.price.toDouble()>0){
                        llMoney.visibility= View.VISIBLE
                        price.text=it.data.self.price
                    }else  llMoney.visibility= View.GONE

                    if (!TextUtils.isEmpty(it.data.packet.hint)){
                        llSend.visibility= View.VISIBLE
                        tips.text=it.data.packet.hint
                    }else {
                        llSend.visibility= View.GONE
                    }
                }

                RefreshHelper.finishRefresh(binding.refreshLayout, pageNo)
                if (pageNo == 1) {
                    mAdapter.replaceData( it.data.list)
                } else {
                    if (!it.data.list.isNullOrEmpty())
                        mAdapter.addData(it.data.list)
                }
            }

        }
    }


    inner class RedExtensionListAdapter  : BaseQuickAdapter<RedDataBean, BaseViewHolder>(
        R.layout.red_item) {

        override fun convert(helper: BaseViewHolder, item: RedDataBean) {
            GlideUtil.ImageHeadLoad(this.context, item.headimg, helper.getView(R.id.headimg))
            helper.setText(R.id.nickname ,item.nickname)
            helper.setText(R.id.price ,item.price)
            helper.setText(R.id.claimtime ,item.claimtime)
            helper.setGone(R.id.tvZuiJia, item.ifzuijia==2)
        }
    }

}